package cards.controller;

import cards.dao.AttendedCardActivityDao;
import cards.dao.UserDao;
import cards.entity.AttendedCardActivity;
import cards.entity.CardActivity;
import cards.entity.User;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import cards.service.CardActivityService;
import cards.util.HostHolder;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;


@Controller
public class AllController {

    @Value("${path.upload}")
    private String uploadPath;
    @Autowired
    private CardActivityService cardActivityService;
    @Autowired
    private HostHolder hostHolder;
    @Autowired
    private AttendedCardActivityDao attendedCardActivityDao;

    // all 是未参与活动详情
    @RequestMapping(path = "/all/{id}", method = RequestMethod.GET)
    public String displaycardActivitiesByUser(@PathVariable String id, Model model) {
        User user = hostHolder.getUser();
        CardActivity cardActivity = cardActivityService.findCardActivityById(id);

        List<String> imglist = new ArrayList<>();

        byte[] fileByte0 = null;
        byte[] fileByte1 = null;
        byte[] fileByte2 = null;
        byte[] fileByte3 = null;
        byte[] fileByte4 = null;
        byte[] fileByte5 = null;

        String img0 = null;
        String img1 = null;
        String img2 = null;
        String img3 = null;
        String img4 = null;
        String img5 = null;

        try {
            String path0 = uploadPath + "\\" + cardActivity.getPublicityPicture();
            String path1 = uploadPath + "\\" + cardActivity.getCardPicture1();
            String path2 = uploadPath + "\\" + cardActivity.getCardPicture2();
            String path3 = uploadPath + "\\" + cardActivity.getCardPicture3();
            String path4 = uploadPath + "\\" + cardActivity.getCardPicture4();
            String path5 = uploadPath + "\\" + cardActivity.getCardPicture5();

            File file0 = new File(path0);
            fileByte0 = Files.readAllBytes(file0.toPath());
            File file1 = new File(path1);
            fileByte1 = Files.readAllBytes(file1.toPath());
            File file2 = new File(path2);
            fileByte2 = Files.readAllBytes(file2.toPath());
            File file3 = new File(path3);
            fileByte3 = Files.readAllBytes(file3.toPath());
            File file4 = new File(path4);
            fileByte4 = Files.readAllBytes(file4.toPath());
            File file5 = new File(path5);
            fileByte5 = Files.readAllBytes(file5.toPath());

            img0 = Base64.encodeBase64String(fileByte0);
            img1 = Base64.encodeBase64String(fileByte1);
            img2 = Base64.encodeBase64String(fileByte2);
            img3 = Base64.encodeBase64String(fileByte3);
            img4 = Base64.encodeBase64String(fileByte4);
            img5 = Base64.encodeBase64String(fileByte5);
        } catch (IOException e) {
            e.printStackTrace();
        }
        imglist.add(img1);
        imglist.add(img2);
        imglist.add(img3);
        imglist.add(img4);
        imglist.add(img5);
        model.addAttribute("user", user);
        model.addAttribute("cardActivity", cardActivity);
        model.addAttribute("imglist", imglist);
        model.addAttribute("publicityImage", img0);
        return "all";
    }

    @RequestMapping(path="/participate/{id}",method=RequestMethod.GET)
    public String participateThisActivity(@PathVariable String id, Model model) {
        User user = hostHolder.getUser();
        CardActivity activity = cardActivityService.findCardActivityById(id);//
        AttendedCardActivity myActivity =
                attendedCardActivityDao.findAttendedCardActivityByTitleAndUsername(activity.getTitle(), user.getUsername());
        if(myActivity != null){
            model.addAttribute("error", "已经参与活动了，不需要再点击参与！");

        }else {
            myActivity =new AttendedCardActivity();
            myActivity.setAvailableScratchTimes(3L);
            myActivity.setTitle(activity.getTitle());
            myActivity.setUsername(user.getUsername());
            attendedCardActivityDao.addAttendedCardActivity(myActivity);
            myActivity = attendedCardActivityDao.findAttendedCardActivityByTitleAndUsername(activity.getTitle(), user.getUsername());
            model.addAttribute("hint", "参与成功！您已获得了3次刮卡机会！");
        }

        List<String> imglist = new ArrayList<>();

        byte[] fileByte0 = null;
        byte[] fileByte1 = null;
        byte[] fileByte2 = null;
        byte[] fileByte3 = null;
        byte[] fileByte4 = null;
        byte[] fileByte5 = null;

        String img0 = null;
        String img1 = null;
        String img2 = null;
        String img3 = null;
        String img4 = null;
        String img5 = null;

        try {
            String path0 = uploadPath + "\\" + activity.getPublicityPicture();
            String path1 = uploadPath + "\\" + activity.getCardPicture1();
            String path2 = uploadPath + "\\" + activity.getCardPicture2();
            String path3 = uploadPath + "\\" + activity.getCardPicture3();
            String path4 = uploadPath + "\\" + activity.getCardPicture4();
            String path5 = uploadPath + "\\" + activity.getCardPicture5();

            File file0 = new File(path0);
            fileByte0 = Files.readAllBytes(file0.toPath());
            File file1 = new File(path1);
            fileByte1 = Files.readAllBytes(file1.toPath());
            File file2 = new File(path2);
            fileByte2 = Files.readAllBytes(file2.toPath());
            File file3 = new File(path3);
            fileByte3 = Files.readAllBytes(file3.toPath());
            File file4 = new File(path4);
            fileByte4 = Files.readAllBytes(file4.toPath());
            File file5 = new File(path5);
            fileByte5 = Files.readAllBytes(file5.toPath());

            img0 = Base64.encodeBase64String(fileByte0);
            img1 = Base64.encodeBase64String(fileByte1);
            img2 = Base64.encodeBase64String(fileByte2);
            img3 = Base64.encodeBase64String(fileByte3);
            img4 = Base64.encodeBase64String(fileByte4);
            img5 = Base64.encodeBase64String(fileByte5);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //判断到底是灰色还是原图
        if(myActivity.getCard1() == null){imglist.add(null);}else {imglist.add(img1);}
        if(myActivity.getCard2() == null){imglist.add(null);}else {imglist.add(img2);}
        if(myActivity.getCard3() == null){imglist.add(null);}else {imglist.add(img3);}
        if(myActivity.getCard4() == null){imglist.add(null);}else {imglist.add(img4);}
        if(myActivity.getCard5() == null){imglist.add(null);}else {imglist.add(img5);}

        model.addAttribute("user", user);
        model.addAttribute("cardActivity", activity);
        model.addAttribute("user", user);
        model.addAttribute("myCardActivity", myActivity);
        model.addAttribute("imglist", imglist);
        model.addAttribute("publicityImage", img0);

        return "detail";

    }

}